package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealOverViewVO;
import com.sky.vo.SetmealVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class SetmealServiceImpl implements SetmealService {

    private static final Logger log = LoggerFactory.getLogger(SetmealServiceImpl.class);
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        // 获取套餐数据以及套餐菜品数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 保存套餐数据
        setmealMapper.insert(setmeal);
        // 保存套餐菜品数据
        setmealDishMapper.insertBatch(setmealDishes, setmeal.getId());
    }

    /**
     * 分页查询
     * @param setmealPageQuery
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQuery) {
        // 判断分页数据是否为空,为空则设置默认值 第一页 五条数据
        if (setmealPageQuery.getPage() == 0) {
            setmealPageQuery.setPage(1);
        }
        if (setmealPageQuery.getPageSize() == 0) {
            setmealPageQuery.setPageSize(5);
        }
        // 开启分页查询
        PageHelper.startPage(setmealPageQuery.getPage(), setmealPageQuery.getPageSize());
        Page<SetmealVO> page = setmealMapper.select(setmealPageQuery);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        // 判断是否存在起售中的套餐
        List<Setmeal> setmealList = setmealMapper.selectByIds(ids);
        for (Setmeal setmeal : setmealList) {
            if (Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        // 删除套餐
        setmealMapper.deleteByIds(ids);
        // 删除套餐菜品数据
        setmealDishMapper.deleteByIds(ids);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        // 查询套餐信息
        List<Setmeal> setmealList = setmealMapper.selectByIds(Collections.singletonList(id));
        if (setmealList == null || setmealList.isEmpty()){
            return null;
        }
        // 查询套餐菜品数据
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        // 组装数据
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmealList.get(0),setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDTO setmealDTO) {
        // 获取套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 获取套餐菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //更新数据
        setmealMapper.update(setmeal);
        setmealDishMapper.deleteByIds(Collections.singletonList(setmeal.getId()));
        if (setmealDishes != null && !setmealDishes.isEmpty()){
            setmealDishMapper.insertBatch(setmealDishes, setmeal.getId());
        }
    }

    /**
     * 启售停售套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        // 判断是起售还是停售
        if (Objects.equals(status, StatusConstant.ENABLE)){
            // 如果是起售,根据id查询菜品是否全部起售
            Long count = setmealDishMapper.selectDishCountBySetmealId(id);
            if (count > 0){
                log.error(MessageConstant.SETMEAL_ENABLE_FAILED);
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        // 封装数据,修改状态
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
